// registerwidget.cpp
#include "registerwidget.h"
#include "loginwidget.h"
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QTcpSocket>
#include <QByteArray>
#include <QCryptographicHash>

RegisterWidget::RegisterWidget(QWidget *parent) :
    QWidget(parent)
{
    setWindowTitle("用户注册");
    resize(300, 200);
    setFixedSize(300, 265);
    QIcon icon(QString(":/new/prefix1/images/register.png"));
    this->setWindowIcon(icon);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // 用户名输入
    QLabel *userLabel = new QLabel("用户名:", this);
    userLabel->setStyleSheet("QLabel {"
                                 "color: #333333;"
                                 "font-size: 16px;"
                                 "font-weight: bold;"
                                 "margin-bottom: 5px;"
                                 "}"); // 标签文字颜

    userLineEdit = new QLineEdit(this);
    userLineEdit->setPlaceholderText("请输入用户名");
    userLineEdit->setStyleSheet("QLineEdit {"
                                "background-color: #f0f0f0;"
                                "border-radius: 5px;"
                                "border: 1px solid #ccc;"
                                "padding: 10px;"
                                "font-size: 14px;"
                                "}"
                                "QLineEdit:focus {"
                                "border: 1px solid #2196F3;"
                                "background-color: #ffffff;"
                                "}");

    // 密码输入
    QLabel *passwordLabel = new QLabel("密码:", this);
    passwordLineEdit = new QLineEdit(this);
    passwordLineEdit->setEchoMode(QLineEdit::Password);
    passwordLineEdit->setPlaceholderText("请输入密码");
    passwordLineEdit->setStyleSheet("QLineEdit {"
                                    "background-color: #f0f0f0;"
                                    "border-radius: 5px;"
                                    "border: 1px solid #ccc;"
                                    "padding: 10px;"
                                    "font-size: 14px;"
                                    "}"
                                    "QLineEdit:focus {"
                                    "border: 1px solid #2196F3;"
                                    "background-color: #ffffff;"
                                    "}");

    // 注册按钮
    registerButton = new QPushButton("注册", this);
    registerButton->setStyleSheet("QPushButton {"
                                  "background-color: #2196F3;"
                                  "color: white;"
                                  "border-radius: 5px;"
                                  "padding: 10px 20px;"
                                  "font-size: 14px;"
                                  "border: none;"
                                  "}"
                                  "QPushButton:hover {"
                                  "background-color: #0b7dda;"
                                  "}");
    concelButton = new QPushButton("取消", this);
    registerButton->setStyleSheet("QPushButton {"
                                  "background-color: #2196F3;"
                                  "color: white;"
                                  "border-radius: 5px;"
                                  "padding: 10px 20px;"
                                  "font-size: 14px;"
                                  "border: none;"
                                  "}"
                                  "QPushButton:hover {"
                                  "background-color: #0b7dda;"
                                  "}");

    // 布局管理
    mainLayout->addWidget(userLabel);
    mainLayout->addWidget(userLineEdit);
    mainLayout->addWidget(passwordLabel);
    mainLayout->addWidget(passwordLineEdit);
    mainLayout->addWidget(registerButton);
    mainLayout->addWidget(concelButton);

    // 信号与槽连接
    connect(registerButton, &QPushButton::clicked, this, &RegisterWidget::onRegisterButtonClicked);
    connect(concelButton, &QPushButton::clicked, this, &RegisterWidget::onconcelButtonButtonClicked);
}

RegisterWidget::~RegisterWidget()
{
}

void RegisterWidget::closeEvent(QCloseEvent *event)
{
    // 当窗口关闭时，返回登录界面

    this->close();  // 隐藏当前注册窗口

    event->accept();  // 接受关闭事件
}

void RegisterWidget::onRegisterButtonClicked()
{
    QString username = userLineEdit->text();
    QString password = passwordLineEdit->text();

    if (username.isEmpty() || password.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请输入用户名和密码");
        return;
    }

    QByteArray encryptedPwd = QCryptographicHash::hash(password.toLatin1(), QCryptographicHash::Sha512);
    QString encryptedPassword = encryptedPwd.toHex();
    QString registerRequest = "REGISTER:" + username + ":" + encryptedPassword;

    sendRequest(registerRequest);
}

void RegisterWidget::onconcelButtonButtonClicked()
{
    LoginWidget *loginWindow = new LoginWidget();
    loginWindow->show();
    this->hide();  // 隐藏当前注册窗口

}

void RegisterWidget::sendRequest(const QString &request)
{
    QTcpSocket *socket = new QTcpSocket(this);

    // 连接到服务器
    socket->connectToHost("10.11.9.30", 2581);
    if (!socket->waitForConnected(500)) {
        QMessageBox::warning(this, "连接错误", "无法连接到服务器");
        return;
    }

    // 发送请求
    socket->write(request.toUtf8());
    socket->flush();

    // 等待服务器响应
    if (socket->waitForReadyRead(3000)) {
        QByteArray response = socket->readAll();
        QString responseStr = QString::fromUtf8(response);

        // 处理服务器响应
        if (responseStr == "SUCCESS") {
            QMessageBox::information(this, "注册成功", "用户注册成功！");
            // 注册成功，返回登录界面
            LoginWidget *loginWindow = new LoginWidget();
            loginWindow->show();
            this->hide();
        } else if (responseStr == "ALREADY RGS") {
            QMessageBox::warning(this, "注册失败", "该用户名已被注册");
        } else {
            QMessageBox::warning(this, "注册失败", "发生未知错误，请稍后再试");
        }
    } else {
        QMessageBox::warning(this, "连接错误", "未收到服务器响应");
    }

    socket->close();
}
